<?

class Smutty_Session {

	var $user = null;

	/**
	 *  constructor
	 *
	 */

	function Smutty_Session() {
		Smutty_Main::loadClass( 'Smutty_Session_User' );
		session_start();
		$this->user = $_SESSION['smutty_user'];
	}

	/**
	 *  returns the instance of the session class
	 *
	 *  @return Smutty_Session
	 *
	 */

	function getInstance() {
		static $instance;
		if ( $instance == null )
			$instance = new Smutty_Session();
		return $instance;
	}

	/**
	 *  STATIC
	 *
	 *  returns a Smutty_Session_User object if there is currently
	 *  a logged in user, otherwise it'll return false
	 *
	 */

	function getUser() {
		$session = Smutty_Session::getInstance();
		if ( !$session->user )
			$session->tryAuth();
		return $session->user ? $session->user : false;
	}

	/**
	 *  tries to authenticate a user.  if the authentication succeeds
	 *  then the $user property will be set accordingly.
	 *
	 */

	function tryAuth() {

		Smutty_Main::loadClass(array(
			'Smutty_Session_Abstract', 'Smutty_Session_Standard'
		));

		$cfg = Smutty_Config::getInstance();

		switch ( $cfg->auth->type ) {
			case 'standard':
				$this->user = Smutty_Session_Standard::getUser();
				break;
		}

		$_SESSION['smutty_user'] = $this->user;

	}

}

?>